﻿using HZH_Controls.Controls;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 渐变.user_Form
{
    public partial class is_paymentForm : Form
    {
        public is_paymentForm()
        {
            InitializeComponent();
        }
        public string opname { get; set; }
        public void loadMsg()
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "RentID", HeadText = "订单编号", Width = 20, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "price", HeadText = "应付金额", Width = 20, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "carname", HeadText = "车名", Width = 50, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "CNumber", HeadText = "车牌号", Width = 50, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "opername", HeadText = "租车人", Width = 20, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Paytime", HeadText = "付款时间", Width = 20, WidthType = SizeType.Percent });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "begindate", HeadText = "起始时间", Width = 40, WidthType = SizeType.Percent });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "returntime", HeadText = "归还时间", Width = 40, WidthType = SizeType.Percent });
            this.ucDataGridView1.Columns = lstCulumns;
            this.ucDataGridView1.IsShowCheckBox = true;
            List<object> lstSource = new List<object>();
            string sql = "select * from carRents where opername=@opername and isPay='True'";
            SqlParameter p = new SqlParameter("@opername", this.opname);
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery(sql, p);
            this.ucDataGridView1.DataSource = dt;
            this.ucDataGridView1.First();
        }

        private void is_paymentForm_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            loadMsg();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {


            if (ucDataGridView1.SelectRows != null && ucDataGridView1.SelectRows.Count > 0)
            {
                var uids = new List<int>();
                foreach (var item in ucDataGridView1.SelectRows)
                {
                    var tempid = Convert.ToInt32((item.DataSource as DataRow)["RentID"]);
                    uids.Add(tempid);
                }
                DialogResult dir = MessageBox.Show("您确定要删除订单吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dir == DialogResult.OK)
                {
                    var uidsstr = new StringBuilder();
                    uidsstr.Append("(");
                    foreach (var item in uids)
                    {
                        uidsstr.Append($"{item},");
                    }
                    uidsstr.Remove(uidsstr.Length - 1, 1);
                    uidsstr.Append(")");
                    string sql = $"delete from carRents where RentID in {uidsstr};";
                    int result = DBHelper.ExecuteNonQuery(sql);
                    if (result > 0)
                    {
                        this.ShowSuccessTip("删除成功！");
                        loadMsg();
                    }
                    else
                    {
                        this.ShowErrorTip("删除失败！");
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                this.ShowErrorTip("未选择任何车辆，请选择要删除的车辆再进行删除!");
            }
            //DialogResult dir = MessageBox.Show("您确定要删除已完成的订单吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            //if (dir == DialogResult.OK)
            //{
            //    string id = (ucDataGridView1.SelectRow.DataSource as DataRow)["RentID"].ToString();

            //    string sql = "delete from carRents where RentID=@RentID";
            //    SqlParameter[] ps =
            //    {
            //        new SqlParameter("@RentID",id),

            //    };
            //    int result = DBHelper.ExecuteNonQuery(sql, ps);
            //    if (result > 0)
            //    {
            //        this.ShowSuccessTip("删除成功！");
            //        loadMsg();

            //    }
            //    else
            //    {
            //        this.ShowErrorTip("删除成功！");
            //    }
            //}
            //else
            //{
            //    return;
            //}
        }

        /*订单投诉*/
        private void btnTousu_Click(object sender, EventArgs e)
        {
            string RentID = (ucDataGridView1.SelectRow.DataSource as DataRow)["RentID"].ToString();
            string carname = (ucDataGridView1.SelectRow.DataSource as DataRow)["carname"].ToString();
            string CNumber = (ucDataGridView1.SelectRow.DataSource as DataRow)["CNumber"].ToString();
            string opername = (ucDataGridView1.SelectRow.DataSource as DataRow)["opername"].ToString();

            user_complaintFrm complaintFrm = new user_complaintFrm();
            complaintFrm.cid = RentID;
            complaintFrm.carname = carname;
            complaintFrm.CNumber = CNumber;
            complaintFrm.opername = opername;
            complaintFrm.ShowDialog();
        }
    }
}
